"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Modal = Modal;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDom = require("react-dom");

var _util = require("../../util");

var _classnames = _interopRequireDefault(require("classnames"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function Modal(props) {
  var clsPrefix = props.clsPrefix,
      triggerNode = props.triggerNode,
      visible = props.visible,
      closeCallback = props.closeCallback,
      state = props.state,
      setState = props.setState,
      unMount = props.unMount,
      children = props.children,
      trigger = props.trigger,
      followTrigger = props.followTrigger,
      _props$onMouseEnter = props.onMouseEnter,
      _onMouseEnter = _props$onMouseEnter === void 0 ? function () {} : _props$onMouseEnter,
      _props$onMouseLeave = props.onMouseLeave,
      _onMouseLeave = _props$onMouseLeave === void 0 ? function () {} : _props$onMouseLeave; // 传送浮层内容至目标容器


  var renderPortal = (0, _react.useMemo)(function () {
    if (visible) {
      var stateCls = "".concat(clsPrefix, "-popover--").concat(state ? "open" : "close"); // className

      var renderContent = /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-popover"), stateCls),
        onClick: function onClick(e) {
          return e.stopPropagation();
        }
      }, children);

      return followTrigger ? renderContent : /*#__PURE__*/(0, _reactDom.createPortal)(renderContent, (0, _util.getRoot)());
    } else {
      unMount();
      return null;
    }
  }, [children, state, unMount, visible]);
  (0, _react.useEffect)(function () {
    closeCallback && closeCallback(setState);
  }, [closeCallback]);
  (0, _react.useEffect)(function () {
    if (!visible) {
      setTimeout(function () {
        unMount();
      }, 100);
    }
  }, [visible, unMount]);
  return /*#__PURE__*/_react.default.createElement("div", {
    onMouseEnter: function onMouseEnter(e) {
      _onMouseEnter();

      if (trigger === "hover") {
        setTimeout(function () {
          setState(true);
        }, 75);
      }
    },
    onMouseLeave: function onMouseLeave(e) {
      _onMouseLeave();

      if (trigger === "hover") {
        setState(false);
        setTimeout(function () {
          setState(false);
        }, 75);
      }
    },
    style: {
      display: "inline-block",
      position: "relative"
    }
  }, renderPortal, triggerNode);
}

var _default = Modal;
exports.default = _default;